const ADSdk = require("ADSdk");
const UIManage = require("UIManage");
cc.Class({
    extends: cc.Component,

    properties: {

        closeBtn: cc.Node,
        openBtn: cc.Node,

        logoSprite: cc.Sprite,
        logoSpriteFrame: [cc.SpriteFrame], // 0新人 1每日 2过关 3恭喜获得
        redPacketNameLabel: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.closeBtn.on("click", this._close, this);
        this.openBtn.on("click", this._open, this);

    },

    init(id) {

        let money = Utils.getRedPacketNum();
        if (money < 88) id = 0;
        this.redId = id;
        let str;
        if (id == 1) {
            str = "根据您的游戏进度已发放红包奖励"
        } else {
            str = "你获得了一个现金红包";
        }

        this.logoSprite.spriteFrame = this.logoSpriteFrame[id];
        this.redPacketNameLabel.string = str;
    },


    Show() {
        this.isPlayingVideo = false;
        this.node.active = true;
    },

    Close() {
        this.isPlayingVideo = false;
        this.node.active = false;
    },

    _close() {
        // this.node.removeFromParent();
        this.Close();
    },

    _open() {

        if (this.isPlayingVideo) return;
        this.isPlayingVideo = true;
        // TODO 广告 视频
        ADSdk.showVideo(null, (result) => {
            if (result == 1) {
                let randomNum = Utils.getRandomRedNum();
                UIManage.Instance.ShowSmallPacket((SmallPacket) => {
                    let hideDouble = false;
                    if (this.redId == 0) hideDouble = true;
                    SmallPacket.init(randomNum, hideDouble);
                });
                this._close();
            }
            this.isPlayingVideo = false;

        })

    },

    // update (dt) {},
});
